SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

/** Create Functions **/
CREATE FUNCTION dbo.[GetACSTransactionId] 
(
  @downloadUrl nvarchar(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
 DECLARE @idx1 INT
 DECLARE @idx2 INT

 SET @idx1 = CHARINDEX(N'&orderid=', @downloadUrl)
 IF (@idx1 = 0) RETURN NULL

 SET @idx2 = CHARINDEX(N'&', @downloadUrl, @idx1 + 1)
 IF (@idx2 = 0) SET @idx2 = LEN(@downloadUrl)

 SET @idx1 = @idx1 + LEN(N'&orderid=')

 SET @downloadUrl = SUBSTRING(@downloadUrl, @idx1, @idx2 - @idx1)

 --RETURN REPLACE(@downloadUrl, N'%24', N'$')
 RETURN dbo.DecodeURL(@downloadUrl)
END

GO
